<?php

//以下代码在命令行里测试通过

require('DB.php'); //只需引入此文件

//获取一个db, 作为一切操作的入口
$db = DB::build('models/', (new MongoClient())->metalab);

$notices = $db->labs->notices->findBy([
    //'fields' => ['name', 'portrait'],
    'cond' => ['labId' => '55228cfbc32c18e80f00002a'],
   // 'orderBy' => '.stat.createTime dsc',
    'skip' => 0,
    'limit' => 10
]);

$noticeList = ($notices['list']);

foreach($noticeList as $notice) {
    $title = $notice['meta']['title'];
    $title = iconv('UTF-8', 'GBK', $title);
    $date = $notice['meta']['date'];
    echo $title.":".$date."\n";
}

echo "\n";

$a = [2, 4, 3, 1];

for($i = 0; $i < 3; $i++) {
    //$i-1之前已经排好了
    $min = $i;
    for($j = $i + 1; $j < 4; $j++) {
        if($a[$j] < $a[$min]) {
            $min = $j;
        }
    }
    $t = $a[$min];
    $a[$min] = $a[$i];
    $a[$i] = $t;
}

print_r($a);

exit(0);

//创建一个user模块
$userId = $db->users->create();

//更新info
$db->users->info->update($userId, [
    'name' => 'xiaoming',
    'portrait' => 'xxx'
]);

//更新info.portrait, 只需传入一个字符串
$db->users->info->portrait->update($userId, 'www');

//find user
$db->users->find($userId);

//find user info
$info = $db->users->info->find($userId);

var_dump($info == ['name' => 'xiaoming', 'portrait' => 'www']);

//find with fields limit
$info = $db->users->info->find($userId, ['name', 'portrait']);

var_dump($info == ['name' => 'xiaoming', 'portrait' => 'www']);

$name = $db->users->info->name->find($userId);

var_dump($name == 'xiaoming');

print_r($db->users->findBy([
    'fields' => ['stat.createTime', 'info.name', 'info.protrait'],
    'cond' => ['info.name' => 'xiaoming'],
    'orderBy' => 'stat.createTime asc',
    'skip' => 0,
    'limit' => 1
]));

print_r($db->users->info->findBy([
    'fields' => ['name', 'portrait'],
    'cond' => ['name' => 'xiaoming'],
    'orderBy' => '.stat.createTime dsc',
    'skip' => 1,
    'limit' => 2
]));

$db->labs->findBy(['skip' => 0, 'limit' => 1]);

$db->labs->papers->findBy(['skip' => 0, 'limit' => 1]);
